package org.wavetech.scrollingcascade;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator
 * 2019/11/7.
 */
public class StatusBarHelper {

    public static void setStatusBarStyle(Activity activity, int color, boolean isLightMode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isLightMode) {
                activity.getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            } else {
                activity.getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            }
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M
                    && color == Color.TRANSPARENT) {
                color = Color.parseColor("#44000000");
            }
            activity.getWindow().setStatusBarColor(color);
        }
    }

    public static int getStatusBarHeight() {
        Resources resources = Resources.getSystem();
        int resId = resources.getIdentifier("status_bar_height", "dimen", "android");
        return resources.getDimensionPixelSize(resId);
    }

    public static void setPaddingTop(View view) {
        //新paddingTop = 原paddingTop + 状态栏高度。
        int paddingTop = view.getPaddingTop() + getStatusBarHeight();
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        //如果高度被写死的话，高度也同样增加 一个状态栏 的高度，否则内容会显示不全。
        if (layoutParams.height != ViewGroup.LayoutParams.WRAP_CONTENT)
            layoutParams.height += getStatusBarHeight();
        //重新设置 padding 值。
        view.setPadding(view.getPaddingLeft(), paddingTop, view.getPaddingRight(), view.getPaddingBottom());
    }
}